/**
 * 
 */
package ro.dta.idbi.patterns;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ro.dta.idbi.interfaces.IntegrationOperation;
import ro.dta.idbi.model.AbstractPattern;
import ro.dta.idbi.model.Utils;
import uk.ac.ic.doc.automed.IntegrityException;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;

/**
 * <b>Redundant column removal transformation pattern</b>
 * 
 * Example of use:<br />
 * <code>redundant_col_removal (&lt;&lt;parent_table,col&gt;&gt;, &lt;&lt;child_table,col&gt;&gt;)</code>
 * <br />
 * 
 * The redundant column <i>col</i> in the <i>child_table</i> is removed
 * 
 * @author Tudor Dobrila
 *
 */
public class RedundantCol extends AbstractPattern {

	/**
	 * Name of the pattern
	 */
	public static final String NAME = "Redundant column removal";

	/**
	 * Description of the pattern
	 */
	public static final String DESCRIPTION = "Remove the redundant attribute from the child table";

	/**
	 * Name of the command associated with the pattern
	 */
	public static final String COMMAND = "redundant_col_removal";

	/**
	 * Integration phase during which the command is applied
	 */
	public static final EnumSet<IntegrationOperation> OPERATION = EnumSet
			.of(IntegrationOperation.IMPROVEMENT);

	/**
	 * Column in the parent table
	 */
	private String parentCol;

	/**
	 * Column in the child table, that is removed
	 */
	private String childCol;
	
	/**
	 * Redundant column removal pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Source schema over which the transformation is applied
	 */
	public RedundantCol(final Schema sourceSchema) throws NotFoundException, IntegrityException {
		super(sourceSchema);

		pattern = Pattern.compile("^" + COMMAND + "[ ]*\\((" + COL_REGEX + ")" + COMMA_REGEX
				+ "(" + COL_REGEX + ")\\)$");
	}

	/**
	 * Redundant column removal pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Name of source schema over which the transformation is applied
	 */
	public RedundantCol(final String sourceSchema) throws NotFoundException, IntegrityException {
		this(Schema.getSchema(sourceSchema));
	}
	
	/* (non-Javadoc)
	 * @see ro.dta.idbi.model.AbstractPattern#parse(java.lang.String)
	 */
	@Override
	protected void parse(String trans) {
		Matcher matcher = pattern.matcher(trans);
		matcher.find();
		
		parentCol = matcher.group(1);
		childCol = matcher.group(2);
	}

	/* (non-Javadoc)
	 * @see ro.dta.idbi.model.AbstractPattern#getPrimitiveCommands()
	 */
	@Override
	protected List<String> getPrimitiveCommands() throws NotFoundException, IntegrityException {
		List<String> result = new ArrayList<String>();
		SchemaObject childColObj = sourceSchema.getSchemaObject(childCol);
		Object[] childColDef = childColObj.getSchemeDefinition();
		Map.Entry<String, String> childColPair = parseColName(childCol);
		
		// Step 1: remove the column from the child table
		StringBuilder extent = new StringBuilder("[ {x,y} | {x} <- ");
		extent.append("<<").append(childColPair.getKey()).append(">>; {x,y} <- ");
		extent.append(parentCol).append(" ]");
		
		StringBuilder cmd = new StringBuilder();
		cmd.append("delete (");
		cmd.append(Utils.genColRepresentation(childColPair.getKey(), childColDef[1], childColDef[2], childColDef[3]));
		cmd.append(", ").append(extent.toString()).append(")");
		result.add(cmd.toString());
		
		return result;
	}

	/* (non-Javadoc)
	 * @see ro.dta.idbi.model.AbstractPattern#verify()
	 */
	@Override
	protected boolean verify() {
		// TODO Auto-generated method stub
		return true;
	}

}
